﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GoodsInvoicing.Views.View;
using GoodsInvoicing.Presentions.Presenter;
using GoodsInvoicing.BLL.Service;
using GoodsInvoicing.Views.ViewImpl;
using GoodsInvoicing.Model.User;

namespace GoodsInvoicing.Presentions.PresenterImpl
{
    public class LoginFormPresenter : ILoginFormPresenter
    {
        #region 构造
        /// <summary>
        /// 无接口的构造被阻止
        /// </summary>
        private LoginFormPresenter()
        {
            throw new NotSupportedException("无接口的实现.");
        }
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="view">视图接口</param>
        public LoginFormPresenter(ILoginForm view, IGI_UserService userService)
        {
            this.view = view;
            this.userService = userService;
        }
        #endregion

        #region 属性
        private ILoginForm view;
        /// <summary>
        /// 视图接口
        /// </summary>
        public ILoginForm View
        {
            get{ return view;}
        }

        private IGI_UserService userService;
        public IGI_UserService UserService 
        { 
            get { return userService; } 
        }
        #endregion

        #region 方法
        /// <summary>
        /// 初始化视图
        /// </summary>
        public void InitializeView()
        {
            this.View.LoginButtonClick += new EventHandler<EventArgs>(View_LoginButtonClick);
        }        
        #endregion

        private void View_LoginButtonClick(object sender, EventArgs e) 
        {
            if(this.View.LoginName != "" && this.View.LoginPWD != "")
            {
                if (userService.Login(this.View.LoginName, this.View.LoginPWD))
                {
                    //View_FormClose(this, e);

                    //MainForm mainForm = new MainForm();
                    //mainForm.Show();

                    UserDetail user = new UserDetail(this.View.LoginName, this.View.LoginName);

                    this.View.MainFormShow(user, null);
                    this.View.LoginFormClose();

                    this.View.IsLogin = true;//登录成功
                    this.View.LoginName = "";//用户名置空
                    this.View.LoginPWD = "";//密码置空
                }
            }
        }
    }
}
